#!/usr/bin/env bash
set -eo pipefail

declare -r SCRIPT_DIR=$(cd $(dirname $0) >/dev/null 2>&1 && pwd)
source ${SCRIPT_DIR}/common.sh

declare -r tag=$1 container_name=$2 data=$3 config=$4 logs=$5

declare -ra docker_run_influxd=(
    docker run -i -d
    --name=${container_name}
    -u $(id -u):influxdb
    -p 8086:8086
    -v ${data}:/var/lib/influxdb2
    -v ${config}:/etc/influxdb2
    -e DOCKER_INFLUXDB_INIT_MODE=setup
    -e DOCKER_INFLUXDB_INIT_USERNAME=${TEST_USER}
    -e DOCKER_INFLUXDB_INIT_PASSWORD=${TEST_PASSWORD}
    -e DOCKER_INFLUXDB_INIT_ORG=${TEST_ORG}
    -e DOCKER_INFLUXDB_INIT_BUCKET=${TEST_BUCKET}
    -e DOCKER_INFLUXDB_INIT_RETENTION=${TEST_RETENTION_SECONDS}s
    -e DOCKER_INFLUXDB_INIT_ADMIN_TOKEN=${TEST_ADMIN_TOKEN}
    influxdb:${tag} influxd run
)

log_msg Booting 2.x container in setup mode
if ! ${docker_run_influxd[@]} > /dev/null; then
    log_msg Error: Failed to launch container
    exit 1
fi
wait_container_ready

# Check that the DB reports it's been set up.
log_msg Checking onboarding API post-start
declare onboarding_allowed=$(curl -s localhost:8086/api/v2/setup | jq .allowed)
if [[ ${onboarding_allowed} != 'false' ]]; then
    log_msg Error: Onboarding allowed post-start
    exit 1
fi


# Make sure we can use the auth token we passed in to find the resources we expect.
log_msg Checking org list post-setup
declare orgs=$(curl -s -H "Authorization: Token ${TEST_ADMIN_TOKEN}" localhost:8086/api/v2/orgs | jq -r .orgs[].name)
if [[ ${orgs} != ${TEST_ORG} ]]; then
    log_msg Error: Bad org list post-setup
    echo ${orgs}
    exit 1
fi

log_msg Checking bucket list post-setup
declare buckets=$(curl -s -H "Authorization: Token ${TEST_ADMIN_TOKEN}" localhost:8086/api/v2/buckets?name=${TEST_BUCKET} | jq -r .buckets[].name)
if [[ ${buckets} != ${TEST_BUCKET} ]]; then
    log_msg Error: Bad bucket list post-setup
    echo ${buckets}
    exit 1
fi

# Destroy the container
log_msg Tearing down 2.x container
docker stop ${container_name} > /dev/null
docker logs ${container_name} > ${logs}/init-docker-stdout.log 2> ${logs}/init-docker-stderr.log
docker rm ${container_name} > /dev/null

# Check that files were persisted to the host.
if [ ! -f ${data}/influxd.bolt ]; then
    log_msg Error: BoltDB not persisted to host directory
    exit 1
fi
if [ ! -f ${config}/influx-configs ]; then
    log_msg Error: CLI configs not persisted to host directory
    exit 1
fi

# Create a new container using the same mount-points and env.
# The INIT_MODE should be ignored since the bolt file is present.
log_msg Booting another 2.x container
if ! ${docker_run_influxd[@]} > /dev/null; then
    log_msg Error: failed to launch container
    exit 1
fi
wait_container_ready

# Make sure the DB still reports that it's been set up.
log_msg Checking onboarding API after recreating container
onboarding_allowed=$(curl -s localhost:8086/api/v2/setup | jq .allowed)

if [[ ${onboarding_allowed} != 'false' ]]; then
    log_msg Error: Onboarding allowed after recreating container
    exit 1
fi

# Make sure we can still use the generated auth token to find the resources we expect.
log_msg Checking org list after recreating container
declare orgs=$(curl -s -H "Authorization: Token ${TEST_ADMIN_TOKEN}" localhost:8086/api/v2/orgs | jq -r .orgs[].name)
if [[ ${orgs} != ${TEST_ORG} ]]; then
    log_msg Error: Bad org list post-setup
    echo ${orgs}
    exit 1
fi

log_msg Checking bucket list after recreating container
declare buckets=$(curl -s -H "Authorization: Token ${TEST_ADMIN_TOKEN}" localhost:8086/api/v2/buckets?name=${TEST_BUCKET} | jq -r .buckets[].name)
if [[ ${buckets} != ${TEST_BUCKET} ]]; then
    log_msg Error: Bad bucket list post-setup
    echo ${buckets}
    exit 1
fi

log_msg Checking bucket RP
declare rp=$(curl -s -H "Authorization: Token ${TEST_ADMIN_TOKEN}" localhost:8086/api/v2/buckets?name=${TEST_BUCKET} | jq -r .buckets[].retentionRules[].everySeconds)

if [[ ${rp} != ${TEST_RETENTION_SECONDS} ]]; then
    log_msg Error: Bad bucket RP post-setup
    echo ${rp}
    exit 1
fi
